package com.oracle.cmp.service;


import com.oracle.cmp.common.Constant;
import com.oracle.cmp.common.Result;
import com.oracle.cmp.entity.Customer;
import com.oracle.cmp.entity.User;
import com.oracle.jdbc.util.Dao;
import org.apache.commons.codec.digest.DigestUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


public class UserServiceImpl implements UserService{

	@Override
	public Result save(User user) {
		Dao.executeSql("insert into sys_user values(default,?,?,?,?)",user.getLoginname(),entoMD5(user.getLoginpwd()),user.getLogintime(),user.getEid()
				);
		return new Result(true,null,"保存成功");
	}

	/**
	 * 按客户名查询客户
	 * @return
	 */

	public static String entoMD5(String str){

		return DigestUtils.md5Hex(str);
	}
	public Result queryForPage(Map<String,Object> map, int pageNum){
		String sql = "select * from sys_user where 1=1";
		List params = new ArrayList();
		if(map.get("LoginName")!=null&&!"".equals(map.get("LoginName"))){
			sql += " and LoginName = ?";
			params.add(map.get("LoginName"));
		}if(map.get("userName")!=null&&!"".equals(map.get("userName"))){
			sql += " and UserName = ?";
			params.add(map.get("userName"));
		}
		sql+=" limit ?,?";

		params.add((pageNum-1)*Constant.PAGESIZE);
		params.add(Constant.PAGESIZE);
		List<User> list = Dao.query(sql,User.class,params.toArray());
		return new Result(true,list,"查询成功");

	}
	/**
	 * 按id查询客户
	 */
	public Result selectById(int userid) {
		Customer customer = Dao.queryOne("select * from sys_user where UserId=?",Customer.class,userid);
		return new Result(true,customer,"查询成功");
	}



	public Result update(User user) {
		return new Result(true,null,"修改成功");
	}
	
	public Result delete(int userid) {
		Dao.executeSql("delete from sys_user where UserId=?",userid);
		return new Result(true,null,"删除成功");
	}
	public int count(Map<String,Object> map){
		String sql = "select count(0) from sys_user where 1=1";
		List params = new ArrayList();
		if(map.get("LoginName")!=null&&!"".equals(map.get("LoginName"))){
			sql += " and LoginName = ?";
			params.add(map.get("LoginName"));
		}if(map.get("userName")!=null&&!"".equals(map.get("userName"))){
			sql += " and UserName = ?";
			params.add(map.get("userName"));
		}
		int total = ((Long)Dao.queryOne(sql,params.toArray())[0]).intValue();
		return total;
	}
}
